package com.tao.object_;

public class EqualsExercise01 {
    public static void main(String[] args) {
        Person person1 = new Person("smith", 20, '男');
        Person person2 = new Person("smith", 20, '男');

        //如果不重写equals方法，默认调用父类Object的equals方法，那么此处应该输出false
        System.out.println(person1.equals(person2));

    }
}

class Person{
    private String name;
    private int age;
    private char gender;

    @Override
    public boolean equals(Object obj) {
        if(this == obj){
            return true;
        }
        if(obj instanceof Person){
            Person person = (Person)obj;//向下转型,需要得到Obj的各个属性
            return this.name.equals(person.name) && this.age == person.age && this.gender == person.gender;
        }
        return false;
    }

    public Person(String name, int age, char gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }
}
